import re
import requests
import subprocess
from functools import partial

subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')
import execjs

cookies = {
    'ihkYnttrQXfVO': '',
    'ihkYnttrQXfVP': '',
}
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'Referer': 'https://sugh.szu.edu.cn/',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

url = 'https://sugh.szu.edu.cn'

request_session = requests.session()
request_session.headers.update(headers)


def getResponseOne():
    response = request_session.get(url)
    print(response)  # 第一次的响应状态码为 412
    cookies['ihkYnttrQXfVO'] = response.cookies['ihkYnttrQXfVO']
    request_session.cookies.update(cookies)
    # print(response.text)
    js_src = re.findall(r'charset="iso-8859-1" src="(.*?)" r=', response.text)[
        0]  # 外链中的js文件 也就是断点的第一处展示的以$_ts = window['$_ts'];开头的代码的链接  后面还需要请求这个链接拿到代码
    # print('js_src:', js_src)
    metaContent = re.findall(r'<meta content="(.*?)"><!--', response.text)[0]  # meta标签的content值
    # print('metaContent:', metaContent)
    autoJsCode = re.findall(r'<script type="text/javascript" r=\'m\'>(.*?)</script>', response.text)[0]  # 自执行的js代码
    # print('autoJsCode:', autoJsCode)
    return js_src, metaContent, autoJsCode


def getTsCode(jsSrc):
    js_url = url + jsSrc
    response = request_session.get(js_url)
    request_session.cookies.update(cookies)
    # print(response.text)
    print('cookies1:', cookies)
    return response.text  # 获取以$_ts = window['$_ts'];开头的代码


def getResponseTwo(metaContent, tsCode, autoJsCode):
    with open('rs5.js', 'r', encoding='utf-8') as js_file:
        js_text = js_file.read()
        js_text = js_text.replace('metaContent', metaContent)
        js_text = js_text.replace("'tsCode'", tsCode)
        js_text = js_text.replace("'autoJsCode'", autoJsCode)
        # 将js_text写入一个新的js文件
        # with open('rs5_new.js', 'w', encoding='utf-8') as new_js_file:
        #     new_js_file.write(js_text)
        js = execjs.compile(js_text)
        cookies['ihkYnttrQXfVP'] = js.call('getCookie').split(';')[0].split('=')[1]
        # cookies['ihkYnttrQXfVP'] = js.call('getCookie').replace('ihkYnttrQXfVP=', '')
        request_session.cookies.update(cookies)
        response = request_session.get(url)
        print('cookies2:', cookies)
        print(response)
        print(response.text)


if __name__ == '__main__':
    jsSrc, metaContent, autoJsCode = getResponseOne()
    tsCode = getTsCode(jsSrc)
    getResponseTwo(metaContent, tsCode, autoJsCode)
